Skip to content

Dubbo基础使用

参考:

一、基本介绍

基本的一些介绍:

Apache Dubbo 是一个高性能、轻量级的开源 Java RPC(Remote Procedure Call,远程过程调用)框架。

它提供了三大核心能力: 面向接口的远程方法调用、智能容错和负载均衡以及服务自动注册和发现。

通过 Dubbo 的方式调用接口服务的逻辑见如下,一般来说,服务提供方和服务消费方会向注册中心注册服务,然后服务消费方会通过 dubbo 的方式调用公有模块下的 接口方法,实现方法在服务提供方下。

  • RPC 调用方式
    • RPC允许一个程序调用另一个位置(通常是远程服务器)上的函数或方法,就像调用本地方法一样,隐藏了网络通信的细节
  • Dubbo 组件
    • 服务提供方
      • 对外提供了一个提供Rpc 接口的服务模块 qwe
      • 在业务实现模块中进行接口实现,实现类添加 Dubbo 的注解
      • 引入注册中心,Dubbo会在配置路径下查找服务实现类,并自动注册到注册中心
    • 服务调用方
      • 第三方系统引入这个服务模块 qwe 依赖
      • 添加 dubbo 和 注册中心的依赖,进行服务调用
      • 根据 Dubbo 的约定俗成的方式调用接口

以下是 Dubbo 的基础使用方法,包括服务提供者(Provider)和服务消费者(Consumer)的配置。

使用示例后续待整合给一个比较OK的示例 to be contined....

基础使用示例

在 Spring Boot 项目中使用 Dubbo 进行远程服务调用涉及到几个步骤:定义服务接口、创建服务提供者(Provider)、创建服务消费者(Consumer)以及配置 Dubbo 和注册中心(通常使用 Zookeeper)。

服务提供

1. 添加依赖

首先,确保在服务提供者和服务消费者的 pom.xml 文件中添加了必要的依赖。

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- Dubbo Spring Boot Starter -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.8</version>
    </dependency>
    <!-- Zookeeper Starter -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper</artifactId>
        <version>2.7.8</version>
        <type>pom</type>
    </dependency>
</dependencies>
2. 定义服务接口

在一个公共模块中定义服务接口,这样服务提供者和消费者都能访问到它。

public interface GreetingService {
    String sayHello(String name);
}

提供公共模块的方式,可以是在系统A内容提供一个公共模块,然后将定义接口放置在该模块中;

如果系统B 需要通过 RPC 方式调用该接口方法,需要引入该定义服务接口模块的依赖,然后再通过 dubbo 的方式调用 系统 A 的接口方法。

3. 创建服务提供者

服务提供者实现了服务接口,并使用 @Service 注解将其注册为 Dubbo 服务。

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;

@DubboService(version = "1.0.0", interfaceClass = GreetingService.class)
public class GreetingServiceImpl implements GreetingService {
    @Value("${dubbo.application.name}")
    private String serviceName;

    @Override
    public String sayHello(String name) {
        return String.format("[%s]: Hello, %s", serviceName, name);
    }
}

application.properties 中配置服务提供者的 Dubbo 和 Zookeeper 信息。

# Dubbo application name
dubbo.application.name=provider-service
# Registry center address
dubbo.registry.address=zookeeper://localhost:2181
# Dubbo protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

服务消费

4. 创建服务消费者

服务消费者使用 @DubboReference 注解来引用远程服务。

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Component
public class GreetingServiceConsumer {
    @DubboReference(version = "1.0.0")
    private GreetingService greetingService;

    public String sayHello(String name) {
        return greetingService.sayHello(name);
    }
}

application.properties 中配置服务消费者的 Dubbo 和 Zookeeper 信息。

# Dubbo application name
dubbo.application.name=consumer-service
# Registry center address
dubbo.registry.address=zookeeper://localhost:2181
5. 测试服务调用

最后,在服务消费者项目中,你可以创建一个 REST 控制器或命令行运行器来测试服务调用。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApplication implements CommandLineRunner {

    @Autowired
    private GreetingServiceConsumer greetingServiceConsumer;

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        String hello = greetingServiceConsumer.sayHello("Dubbo");
        System.out.println(hello);
    }
}

注意事项

  • 确保 Zookeeper 服务已经启动并且监听在正确的端口上。
  • 服务提供者和消费者都需要在其 application.propertiesapplication.yml 文件中配置 Dubbo 和 Zookeeper 的连接信息。
  • 使用 @DubboService@DubboReference 注解时,确保版本信息一致,以便正确引用服务。